********************************************************************************
*   Project: Paper 3 - Diabetes Continuum of Care					  *
*   Task: 			   								  *
*	Author: Dina Goodman (Harvard University)      							   *
*	Code check:      														   *
*   Date: 29 April 2021													   *
*	Last updated: 7 Nov 2021											   *
********************************************************************************
///Data cleaning
use "/Users/dinagoodman/Dropbox/EVESCAM/Data/EVESCAM raw_with parish.dta", clear
global Data "/Users/dinagoodman/Dropbox/Harvard/Dissertation/Paper 3/Data"
cd "$Data"
svyset Parroquia [pweight = weighted_age_sex_region]

replace Region=1 if Code=="L110"

*Recoding age
drop agecat
recode Age (20/39.999 = 1 "<40") (40/49.9999 = 2 "40-49") (50/59.9999 = 3 "50-59") (60/97 = 4 "60+"), gen(agecat)
label variable agecat "Age Cat"
recode Age (20/59.999 = 1 "<60") (60/97 = 2 "60+"), gen(age2cat)
recode agecat (1 2 = 1 "<50") (3=2 "50-59") (4 = 3 "60+"), gen (age3cat)

*Urban Rural
recode Locality (1=0 "Rural") (2=1 "Urban"), gen(urban)
*New SES
drop ses
recode SocioEconStatus (1 2 = 1 "High & Med-High") (3 = 2 "Medium") (4 5= 3 "Relative & Extreme Poverty"), gen(ses)


*Type 2 Diabetes
*Clinical diabetes - self
gen long clindia_self=.
replace clindia_self=1 if PersonalDiabetes==1 | (GlucosePostLoad>=200 & GlucosePostLoad!=.) | (FastingGlucose>=126 & FastingGlucose!=.) 
replace clindia_self=0 if (GlucosePostLoad<200 |  GlucosePostLoad==.) & (FastingGlucose<126 | FastingGlucose==.) & (PersonalDiabetes==2 | PersonalDiabetes==.)
replace clindia_self=. if GlucosePostLoad==. & FastingGlucose==. & PersonalDiabetes==.

*Undiagnosed Diabetes
gen long unddia=.
replace unddia=1 if clindia_self==1 & (PersonalDiabetes==2)
replace unddia=0 if PersonalDiabetes==1 
label variable unddia "Undiagnosed diabetes"
label values unddia yesno

*Glycemic control, among those with SELF diagnosed diabetes
gen long control_dia=1
replace control_dia=0 if clindia_self==1 & (FastingGlucose>=154 & FastingGlucose!=.)
replace control_dia=. if clindia_self==. & GlucosePostLoad==. & FastingGlucose==.
label variable control_dia "Glycemic Control"
label values control_dia yesno

*Hypertension Control
gen htn_control=.
replace htn_control=1 if SBPaverage<140 & DBPaverage<90
replace htn_control=0 if (SBPaverage>=140 & SBPaverage!=.) | (DBPaverage>=90 & DBPaverage !=.)
label values htn_control yesno
label variable htn_control "BP Control"

*High LDL:
encode TreatmentDyslipidemia, gen(trt_dys)
replace trt_dys=. if trt_dys==3

gen highLDL=0
replace highLDL=1 if LDL>=100 | trt_dys==1
replace highLDL=. if LDL==. 
label values highLDL yesno

gen ldl_control=.
replace ldl_control=1 if LDL<100
replace ldl_control=0 if LDL>=100 & LDL!=. //stata sets missings as high values
label variable ldl_control "LDL Cholesterol Control"
label values ldl_control yesno

**Nonsmoker
recode Smoker (3 2 = 1 "Non-Smoker") (1 = 0 "Current Smoker"), gen(nonsmk)

*ABC Control
gen abc_control=0
replace abc_control=1 if control_dia==1 & htn_control==1 & ldl_control==1
replace abc_control=. if control_dia==. | htn_control==. | ldl_control==.
label values abc_control yesno
label variable abc_control "ABC Control"

*ABC Control + Nonsmoker
gen abc_nsmk_control=0
replace abc_nsmk_control=1 if control_dia==1 & htn_control==1 & ldl_control==1 & nonsmk==1
replace abc_nsmk_control=. if control_dia==. | htn_control==. | ldl_control==. | nonsmk==.
label values abc_nsmk_control yesno
label variable abc_nsmk_control "Combined ABC Control + Non-Smoker"

*Individual Continuum Steps
***Step 1: All diabetes
gen cont_alldia=0
replace cont_alldia=1 if clindia_self==1
***Step 2
*Aware: Baseline
gen cont_aware=0
replace cont_aware=1 if clindia_self==1 & unddia==0
***Step 3: Treatment
gen cont_trt=0
replace cont_trt=1 if clindia_self==1 & unddia==0 & TreatmentforDiabetes==1 
***Step 4: Glycemic Control
gen cont_control=0
replace cont_control=1 if clindia_self==1 & unddia==0 & TreatmentforDiabetes==1 & control_dia==1
***Step 5: ABC Control
gen cont_abc=0
replace cont_abc=1 if clindia_self==1 & unddia==0 & TreatmentforDiabetes==1 & abc_control==1
***Step 6: ABC Control + Non-Smoker
gen cont_abc_nonsmk=0
replace cont_abc_nonsmk=1 if clindia_self==1 & unddia==0 & TreatmentforDiabetes==1 & abc_control==1 & abc_nsmk_control==1

*Simplified Continuum as categorical variable 
gen score =.
replace score=1 if cont_alldia==1
replace score=2 if cont_alldia==1 & unddia==0
replace score=3 if cont_alldia==1 & unddia==0 & TreatmentforDiabetes==1  
replace score=4 if cont_alldia==1 & unddia==0 & TreatmentforDiabetes==1 & control_dia==1
label define simp_continuum 1 "All " 2 "Aware" 3 "On treatment" 4 "Controlled"
label values score simp_continuum

save "Paper 3_Final BL Dataset_11.7.21.dta", replace

///Analysis with baseline data only
*Figure 1: GET PROPORTIONS TO PUT IN EXCEL
svy: prop clindia_self 
svy, subpop(if clindia_self==1): prop cont_aware
svy, subpop(if clindia_self==1): prop cont_trt
svy, subpop(if clindia_self==1): prop cont_control
svy, subpop(if clindia_self==1): prop cont_abc
svy, subpop(if clindia_self==1): prop cont_abc_nonsmk

*Test p values, not used in paper 
svy, subpop(if clindia_self==1): tabulate clindia_self cont_aware
svy, subpop(if clindia_self==1): tabulate cont_aware cont_trt
svy, subpop(if clindia_self==1): tabulate cont_trt cont_control
svy, subpop(if clindia_self==1): tabulate cont_control cont_abc
svy, subpop(if clindia_self==1): tabulate cont_abc cont_abc_nonsmk


*Tables with weighted proportions:
svy, subpop(if clindia_self==1): prop score

*check sample size before models /Age
table agecat score
svy: mlogit score i.agecat, rrr
//Gender
table Gender score
svy: mlogit score Gender, rrr
//SES
table ses score
svy: mlogit score i.ses, rrr base(1) //extreme poverty, on treatment RRR= 0.27 p=0.008
//Urban vs rural
table urban score
svy: mlogit score urban, rrr base(1)
//overweight
table overweight score
svy: mlogit score overweight, rrr base(1) //aware and control,

//htn
table htn score
svy: mlogit score htn, rrr base(1)
//LDL
table highLDL score
svy: mlogit score highLDL, rrr base(1)


